﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotSpatial.Topology;
using System.Data;
using DotSpatial.Data;
using DotSpatial.Projections;

namespace SoilProfiler
{
    public class Columns
    {

        private List<LayerColumn> cols;

        public List<LayerColumn> Profiles
        {
            get { return cols; }
            set { cols = value; }
        }
        private DataTable Info;
        private DataTable data;

        private   Dictionary<int,Coordinate> listCoor;
        public Columns(DataTable info, ProjectionInfo source)
        {
            this.Info = info;
            cols = new List<LayerColumn>();
            if (info!= null)
             if (this.Info.Rows.Count > 0)
                FillData(source);
        }



        public Columns(DataTable info,Dictionary<int,Coordinate> listCoor,ProjectionInfo source)
            {
                // TODO: Complete member initialization
                this.Info = info;
                cols = new List<LayerColumn>();
                this.listCoor = listCoor;

                if (info != null)
                    if (this.Info.Rows.Count > 0)
                        FillData(source);
      
            }

        
        private void FillData(ProjectionInfo proj )
        {
            foreach (DataRow item in Info.Rows)
            {
                double[] cor = new double[2];
                cor[0]=listCoor[Convert.ToInt32(item["wellid"])].X;
                cor[1]=listCoor[Convert.ToInt32(item["wellid"])].Y;
                Reproject.ReprojectPoints(cor, new double[1] { 0 }, proj, KnownCoordinateSystems.Projected.World.WebMercator, 0, 1);
               
                cols.Add(
                      new LayerColumn(
                          Convert.ToInt32(item["id"]),
                          Convert.ToInt32(item["wellid"]),
                          Convert.ToInt32(item["refelev"]),
                          Convert.ToInt32(item["fromdepth"]),
                          Convert.ToInt32(item["todepth"]),
                          Convert.ToInt32(item["topelev"]),
                          Convert.ToInt32(item["bottomelev"]),
                            Convert.ToString(item["unit"]),
                           Convert.ToString(item["description"]),
                           cor[0],
                            cor[1]

                          
                          )
                    );
            }
        }

        public double MinValue()
        {
            return (from v in cols select v.bottom).Min();
          
        }

        public double MaxValue()
        {
            return  (from v in cols select v.topelev).Max();
        

        }
        public List<LayerColumn> GetProfile(int id)
        {
            IEnumerable<LayerColumn> data = from column in cols where column.Profile == id select column;
            if (id == 6503)
            {
                var p = data.ToList();
                var p1 = data.OrderBy(i => i.topelev).ToArray();
            
            }

            return data.OrderBy(i => i.topelev).Reverse().ToList();
        }






    }

    public class LayerColumn:IComparable,ICloneable
    {
        public string Unit { set; get; }
        public double refelev;
        public double fromdepth;
        public double todepth;
        public double topelev;
        public double bottom;
        public int Order;
        public double x;
        public double y;
        public int Profile { set; get; }
        public int Id { set; get; }
        private Coordinate[] array;
        public string description;
        public LayerColumn(int id, int profile, double re, double fd,double td, double te, double b, string unit,string description,double x, double y)
        {
            this.Id = id;
            this.refelev = re;
            this.fromdepth = fd;
            this.topelev = te;
            this.todepth = td;
            this.bottom = b;
            this.Unit = unit;
            this.Profile = profile;
            this.description = description;
            this.x = x;
            this.y = y;
            array= new Coordinate[5];
        }

        public Polygon GetPolygon(double x,double y,double scaleX)
        {

            array[0] = new Coordinate(x, y+topelev);
            array[1] = new Coordinate(x, y+bottom);
            array[2] = new Coordinate(x+scaleX,y+ bottom);
            array[3] = new Coordinate(x + scaleX, y + topelev);
            array[4] = new Coordinate(x, y+topelev);
            LinearRing shell = new LinearRing(array);
            Polygon poly = new Polygon(shell);
            return poly;
        
        
        }

        public int  CompareTo(object obj)
        {
 	        if (this.fromdepth>= ((LayerColumn)obj).fromdepth)
                return 0;
            else
                return 1;

        }



        public object Clone()
        {
            return new LayerColumn(this.Id, this.Profile, this.refelev, this.fromdepth,this.todepth, this.topelev, this.bottom, this.Unit, this.description, this.x,this.y);
        }
    }
}
